package cn.edu.scnu.controller;

import cn.edu.scnu.annotation.IgnoreToken;
import cn.edu.scnu.service.PicService;
import cn.edu.scnu.vo.PicUploadResult;
import cn.edu.scnu.vo.ResultCode;
import cn.edu.scnu.vo.SysResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Api
@RestController
public class PicController {
    @Autowired
    private PicService picService;

    /**
     * 图片上传
     *
     * @return 返回图片路径名称
     */

    @IgnoreToken
    @PostMapping("/pic/manage/upload")
    @ApiOperation(value = "/pic/manage/upload", notes = "图片上传")
//    @ApiImplicitParam(name="picture", dataType="MultipartFile", required=true)
    public PicUploadResult picUpload(HttpServletRequest request, MultipartFile pic) {
        return picService.picUpload(request,pic);
    }


    /**
     * @param picpath 图片路径
     * @return 返回操作状态码
     */
    @IgnoreToken
    @DeleteMapping("/pic/manage/delpic")
    @ApiOperation(value = "/pic/manage/delpic", notes = "图片删除")
    public SysResult deletePic(String picpath) {
        SysResult result;
        try {
            result = picService.deletePic(picpath);
        } catch (Exception e) {
            e.printStackTrace();
            result = SysResult.build(ResultCode.INTERNAL_SERVER_ERROR, e.getMessage(), null);
        }
        return result;
    }
}
